package com.kobe.algorithm.leetcode.daytest.day1;

public class Case278_FirstBadVersion {

    static boolean versions[] = {false,true};

    public static int firstBadVersion(int n) {
        if(isBadVersion(1)){
            return 1;
        }

        if(!isBadVersion(n)){
            return -1;
        }

        int left = 2,right = n;
        while(left <= right){
            int mid =left+((right-left)%2==0?(right-left)/2:(right-left)/2+1);

            if(!isBadVersion(mid-1) && isBadVersion(mid)){
                return mid;
            }
            if(!isBadVersion(mid)){
                left = mid + 1;
            }else{
                right = mid - 1;
            }
        }
        return -1;
    }

    public static boolean isBadVersion(int n){
        return versions[n-1];
    }

    public static void main(String[] args) {

        int bad = firstBadVersion(versions.length);
        System.out.println("n is "+versions.length+",bad :"+bad);

    }
}
